package com.sxc.xmlAdapter;

import com.sxc.bean.Body;
import com.sxc.bean.BodyPlus;
import com.sxc.bean.Field;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;

import java.lang.reflect.Method;
import java.util.List;

public class BodyAdapter extends XmlAdapter<Body, BodyPlus> {
    @Override
    public BodyPlus unmarshal(Body body) throws Exception {
        BodyPlus bodyPlus = new BodyPlus();
        for(java.lang.reflect.Field reflectField : BodyPlus.class.getDeclaredFields()){

            String value = body.getFieldList().stream()
                    .filter(f -> f.getName().compareToIgnoreCase(reflectField.getName())==0)
                    .map(Field::getValue).findAny().get();

            List<Method> methods = List.of(BodyPlus.class.getDeclaredMethods());
            Method method = methods.stream()
                    .filter(f -> f.getName().compareToIgnoreCase("set" + reflectField.getName()) == 0)
                    .findAny().get();

            method.invoke(bodyPlus,value);
        }

        return bodyPlus;
    }

    @Override
    public Body marshal(BodyPlus bodyPlus) throws Exception {
        return null;
    }
}
